<?php

namespace Frieza\Modules\User\Controllers;

use App\Admin\Extensions\WorkContent;
use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel;
use Biz\model\brandAccount\BrandAccountInfo;
use Biz\model\wiki\WikiList as mWikiList;
use Doraemon\cache\UserCounter;
use Doraemon\model\AdmChkPhoneHistory;
use Doraemon\model\AdmOperateLog;
use Doraemon\model\AdmRecommPhoto;
use Doraemon\model\AdmSaleBind\AdmSaleBind;
use Doraemon\model\AdmUserType;
use Doraemon\model\Article;
use Doraemon\model\Blank;
use Doraemon\model\DecoCompanyAuth;
use Doraemon\model\DesignerAuth;
use Doraemon\model\DesignerOperatorBind\DesignerOperatorBind;
use Doraemon\model\IdeaBook as IdeaBookModel;
use Doraemon\model\Member;
use Doraemon\model\MemberDecoration;
use Doraemon\model\MemberDecorationRecord;
use Doraemon\model\MemberExtendInfo;
use Doraemon\model\Phone;
use Doraemon\model\Qq;
use Doraemon\model\Sina;
use Doraemon\model\Weixin;
use Doraemon\service\CommonEmblem;
use Doraemon\service\DingTalkAlert;
use Doraemon\service\Member as sMember;
use Doraemon\service\Point;
use Doraemon\service\position\PositionApi;
use Doraemon\service\Totem\TotemService;
use Doraemon\service\User;
use Doraemon\tools\Area;
use Doraemon\tools\Area as PlaceArea;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\HhzNiceLivingUrl;
use Doraemon\tools\IdParse;
use Doraemon\tools\ImgUrl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Illuminate\Http\Request;
use hhz\constant\basic\findPhoneScene;
use hhz\constant\basic\UserExtendPhoneScene;

class UserDetailController extends Controller
{
    public static $map_user_quality = [
        '0' => '默认',
        '1' => 'good',
        '2' => 'normal',
        '3' => 'S2',
        '9' => 'B2',
        '10' => 'B1',
        '11' => 'B3',
    ];

    public function index(Request $request, $uid)
    {
        return Admin::content(function (Content $content) use ($uid) {

            $content->header('用户详情页');
            $content->description('在这里将看到用户的详情');

            //$content->row(Dashboard::title());

            $content->row(function (Row $row) use ($uid) {

                $row->column(4, function (Column $column) use ($uid) {
                    $column->append(self::BasicInfo($uid));
                });

                $row->column(3, function (Column $column) use ($uid) {
                    $column->append(self::ProductionInfo($uid));
                });

                $row->column(3, function (Column $column) use ($uid) {
                    $column->append(self::InteractionInfo($uid));
                });
                $row->column(2, function (Column $column) use ($uid) {
                    $column->append(self::UserAction($uid));
                });
            });
        });
    }

    private static function BasicInfo($uid)
    {
        $user_basic_info = User::getUserInfo($uid);
        //拼接加密Uid
        $hash_uid = IdParse::hashUid($uid);
        //中文地址拼接
        $area = Area::locationId2String($user_basic_info["area"]);
        //性别区分
        $gender = "未填写";
        switch ($user_basic_info['gender']) {
            case 1 :
                $gender = "男";
                break;
            case 2 :
                $gender = "女";
                break;
        }
        //处理用户在APP内的二维码信息
        $params = [
            'link' => HhzNiceLivingUrl::getUserDetailLink($uid),
            'type' => 'link',
        ];
        $qr_code = ImgUrl::getQrcode(json_encode($params));
        //获取某个用户的积分
        $integral = Point::getAmount($uid);
        //徽章信息
        if ($user_basic_info['type'] == Member::USER_COMMON || $user_basic_info['type'] == Member::AUTH_USER_V) {
            $emblem_info = TotemService::getAllGetTotem($uid);
            $commonEmblems_num = count($emblem_info);
            $commonEmblems = [];
            if ($commonEmblems_num > 0) {
                $temp = array_column($emblem_info, 'totem_img');
                $commonEmblems = [$temp[0]];
                $commonEmblems_num = count($emblem_info);
            }
        } else {
            $commonEmblems = [];
            $commonEmblems_num = 0;
        }
        //处理用户状态
        $user_status = '';
        switch ($user_basic_info["status"]) {
            case Member::STATUS_NORMAL:
                $user_status = "正常用户";
                break;
            case Member::STATUS_NEW:
                $user_status = "未注册";
                break;
            case Member::STATUS_FROZEN:
                $user_status = "冻结用户";
                break;
            case Member::STATUS_BLOCKED:
                $user_status = "封禁用户";
                break;
            case Member::STATUS_UNREGISTER:
                $user_status = "注销用户";
                break;
        }
        //处理用户身份信息
        $identity_type = Member::$typeDesc[$user_basic_info["type"]];
        //处理认证状态
        $auth_status = "无认证状态";
        $dock_sales = '';
        if ($user_basic_info["type"] == Member::AUTH_USER_BRAND) {
            //处理品牌认证状态
            $where["uid"] = $uid;
            $files = '*';
            $brand_info = BrandAccountInfo::getOneByWhere($where, $files);
            $brand_auth_status = '';
            switch ($brand_info["auth_status"]) {
                case BrandAccountInfo::AUTH_STATUS_DEFAULT:
                    $brand_auth_status = "待认证品牌";
                    break;
                case BrandAccountInfo::AUTH_STATUS_USED:
                    $brand_auth_status = "已认证品牌";
                    break;
                case BrandAccountInfo::AUTH_STATUS_NOPASS:
                    $brand_auth_status = "未认证通过品牌";
                    break;
                case BrandAccountInfo::AUTH_STATUS_EXPIRE:
                    $brand_auth_status = "已过期品牌";
            }
            $auth_status = $brand_auth_status;
            //dd($brand_info);

        } elseif ($user_basic_info["type"] == Member::AUTH_USER_DESIGNER) {
            //处理设计师认证状态
            $designer_auth_info = DesignerAuth::getByUId($uid);
            $designer_auth_status = '';
            switch ($designer_auth_info['auth_status']) {
                case DesignerAuth::STATUS_NO_AUTH:
                    $designer_auth_status = "未认证设计师";
                    break;
                case DesignerAuth::STATUS_AUTH_COMPLETE:
                    $designer_auth_status = "已认证设计师";
                    break;
                case DesignerAuth::STATUS_AUTH_CANCEL:
                    $designer_auth_status = "取消认证设计师";
                    break;
                case DesignerAuth::STATUS_AUTH_EXPIRE:
                    $designer_auth_status = "认证过期设计师";
                    break;
            }
            $auth_status = $designer_auth_status;
            //处理设计师对接的销售
            $sale_info = AdmSaleBind::getInfoByUid($uid);
            if ($sale_info) {
                $dock_sales = $sale_info["admin_name"];
            }
            // 处理设计师对接的运营
            $operator_info = DesignerOperatorBind::getInfoByUid($uid);
            if ($operator_info) {
                $dock_operator = $operator_info["admin_name"];
            }
        } elseif ($user_basic_info["type"] == Member::AUTH_DECO_COMPANY) {
            //处理装修公司认证状态
            $decompany_auth_info = DecoCompanyAuth::getByUid($uid);
            $decompany_auth_status = '';
            switch ($decompany_auth_info["auth_status"]) {
                case DecoCompanyAuth::AUTH_STATUS_UNAUTH:
                    $decompany_auth_status = "未认证装修公司";
                    break;
                case DecoCompanyAuth::AUTH_STATUS_SUCCESS:
                    $decompany_auth_status = "已认证装修公司";
                    break;
                case DecoCompanyAuth::AUTH_STATUS_CANCEL:
                    $decompany_auth_status = "已取消认证装修公司";
                    break;
                case DecoCompanyAuth::AUTH_STATUS_EXPIRED:
                    $decompany_auth_status = "认证到期装修公司";
                    break;
            }
            $auth_status = $decompany_auth_status;
        }
        //处理标签
        $adm_user_type = AdmUserType::getByUid($uid);
        $tag_info = $adm_user_type['identity_mark'];
        //处理用户质量
        $user_quality = '';
        switch ($adm_user_type["quality"]) {
            case AdmUserType::NORMAL:
                $user_quality = "正常";
                break;
            case AdmUserType::B1:
                $user_quality = "B1用户";
                break;
            case AdmUserType::B2:
                $user_quality = "B2用户";
                break;
            case AdmUserType::B3:
                $user_quality = "B3用户";
                break;
            case AdmUserType::XL:
                $user_quality = "XL用户";
                break;
        }
        //处理备注
        $user_remark = $adm_user_type["remark"];
        //处理精选记录
        $recommend_num = AdmRecommPhoto::getCountByUid($uid);
        //处理装修状态
        $decoration_status = MemberExtendInfo::getDecorationStatus($uid);
        $user_decoration_info = '';
        switch ($decoration_status) {
            case MemberExtendInfo::STATUS_DECORATION_PLAN:
                $user_decoration_info = "准备装修";
                break;
            case MemberExtendInfo::STATUS_DECORATION_ON:
                $user_decoration_info = "正在装修";
                break;
            case MemberExtendInfo::STATUS_DECORATION_INROOM:
                $user_decoration_info = "已经入住";
                break;
            case MemberExtendInfo::STATUS_DECORATION_NOPLAN:
                $user_decoration_info = "暂无计划";
                break;
        }
        //处理注册时间
        $user_register_time = date("Y-m-d H:i:s", $user_basic_info["addtime"]);
        //处理手机号
        $user_phone = "";
        $phone_info = Phone::getByUid($uid);
        if ($phone_info) {
            $user_phone = $phone_info["hidden_phone"];
        } else {
            $user_phone = "未绑定";
        }

        //获取启动日天数
        $start_days = sMember::getUserSartDays($uid);

        //处理是否绑定微信
        $is_bind_weixin = Weixin::getOpenidByUid($uid);
        $weixin_bind_info = "未绑定";
        if ($is_bind_weixin) {
            $weixin_bind_info = "已绑定";
        }
        //处理是否绑定微博
        $is_bind_sina = Sina::getSinaUidByUid($uid);
        $sina_bind_info = "未绑定";
        if ($is_bind_sina) {
            $sina_bind_info = "已绑定";
        }
        //处理是否绑定了QQ
        $is_bind_qq = Qq::getOpenidByUid($uid);
        $qq_bind_info = "未绑定";
        if ($is_bind_qq) {
            $qq_bind_info = "已绑定";
        }


        //dd($user_basic_info);
        $envs = [
            ['name' => '用户头像', 'value' => $user_basic_info['avatar']],
            ['name' => '用户昵称', 'value' => $user_basic_info['nick']],
            ['name' => '加密ID', 'value' => $hash_uid],
            ['name' => '用户Uid', 'value' => $uid],
            ['name' => '地区', 'value' => $area],
            ['name' => '性别', 'value' => $gender],
            ['name' => '生日', 'value' => $user_basic_info['birthday']],
            ['name' => '用户信息二维码', 'value' => $qr_code],

            ['name' => '个人简介', 'value' => $user_basic_info['profile']],
            ['name' => '积分', 'value' => $integral],
            ['name' => '徽章', 'value' => $commonEmblems[0]],

            ['name' => '状态', 'value' => $user_status],
            ['name' => '用户身份', 'value' => $identity_type],
            ['name' => '认证状态', 'value' => $auth_status],
            ['name' => '对接销售', 'value' => $dock_sales],
            ['name' => '对接运营', 'value' => $dock_operator],
            ['name' => '标签', 'value' => $tag_info],
            ['name' => '用户质量', 'value' => $user_quality],
            ['name' => '备注', 'value' => $user_remark],
            ['name' => '精选记录', 'value' => $recommend_num],
            ['name' => '装修状态', 'value' => $user_decoration_info],
            ['name' => '注册时间', 'value' => $user_register_time],
            ['name' => '手机号', 'value' => $user_phone],
            ['name' => '启动日', 'value' => $start_days],
            ['name' => '微信号', 'value' => $weixin_bind_info],
            ['name' => '微博号', 'value' => $sina_bind_info],
            ['name' => 'QQ', 'value' => $qq_bind_info],

        ];
        if ($user_basic_info["type"] == Member::AUTH_USER_DESIGNER) {
            //处理设计师的相关信息
            //处理荣耀值
            $honor_score = $user_basic_info["glory_data"]["glory"];
            //处理服务地区
            $service_area = $user_basic_info["service_area"];
            //处理设计报价
            $price_range = $user_basic_info["price_range"];
            //处理服务范围
            $service_scope = $user_basic_info["service_scope"];

            $designer_extra_info = [
                ['name' => '荣耀值', 'value' => $honor_score],
                ['name' => '服务地区', 'value' => $service_area],
                ['name' => '设计报价', 'value' => $price_range],
                ['name' => '服务范围', 'value' => $service_scope],

            ];
            array_splice($envs, 12, 0, $designer_extra_info);
        }
        //dd($envs);

        return view('user::userdetail-basic', [
            'envs' => $envs,
            'commonEmblems_num' => $commonEmblems_num,
            'uid' => $uid
        ]);
    }

    private static function ProductionInfo($uid)
    {
        //处理用户生产内容数据
        $user_counter = User::get_counter($uid);
        //处理note数
        $note_num = $user_counter["photo"];
        //处理整屋案例数
        $article_num = $user_counter["article"];
        //处理经验文章数
        $blank_num = $user_counter["blank"];
        //处理整屋草稿数量
        $article_draft_where = [
            "uid" => $uid,
            "status" => [Article::STATUS_FIRST_EDIT, Article::STATUS_DRAFT, Article::STATUS_ONE_KEY_DRAFT]
        ];
        $article_draft_num = Article::getCountByWhere($article_draft_where);
        //处理经验文章草稿数量
        $blank_draft_where = [
            "uid" => $uid,
            "status" => [Blank::STATUS_FIRST_EDIT, Blank::STATUS_DRAFT],
        ];
        $blank_draft_num = Blank::getCountByFilter($blank_draft_where);

        //处理回答数
        $answer_num = $user_counter['answer'];
        //处理wiki数量
        $wiki_num = mWikiList::getUserWikiCount($uid);
        //处理最后生产时间
        $lastReleaseTime = CommonEmblem::getLastReleaseDays($uid);
        $lastReleaseTime_date = self::timeFormat($lastReleaseTime);

        //dd($user_counter);
        //整屋note链接
        $note_link = '/admin/photo/photocheck/index?&audit_again=0&search_photo_type=1&relate_goods=0&uid=' . $uid . '&search_tag=&tag_status=1&photo_num=1';
        //整屋草稿链接
        $article_draft_link = '/admin/article/select/draftlist?Find_group=1&Find=' . $uid . '&status=1';
        //经验草稿链接
        $blank_draft_link = '/admin/blank/select/draftlist?&Find_group=1&Find=' . $uid . '&status=1';
        //回答数链接
        $answer_link = '/admin/photo/answer/list?&uid=' . $uid;
        //wiki数链接
        $wiki_link = 'https://beerus.haohaozhu.me/admin/wiki/shareWiki?&wiki_id=&uid=' . $uid;


        $product_info = [
            'Note' => [
                'name' => 'note数',
                'num' => $note_num,
                'link' => $note_link,
            ],
            'Article' => [
                'name' => '整屋案例数',
                'num' => $article_num,
                'link' => '/admin/article/select/index?&uid=' . $uid,
            ],
            'Blank' => [
                'name' => '经验文章数',
                'num' => $blank_num,
                'link' => '/admin/blank/select/index?&uid=' . $uid,
            ],
            'Article-Draft' => [
                'name' => '整屋草稿',
                'num' => $article_draft_num,
                'link' => $article_draft_link,
            ],
            'Blank-Draft' => [
                'name' => '经验草稿',
                'num' => $blank_draft_num,
                'link' => $blank_draft_link,
            ],
            'Answer' => [
                'name' => '回答数',
                'num' => $answer_num,
                'link' => $answer_link,
            ],
            'Wiki_num' => [
                'name' => 'wiki数',
                'num' => $wiki_num,
                'link' => $wiki_link,
            ],
            'LastProductTime' => [
                'name' => '最后生产时间',
                'num' => $lastReleaseTime_date,
                'link' => 'https://github.com/laravel-admin-extensions/reporter',
            ],
        ];

        return view('user::userdetail-product', [
            'product_info' => $product_info,
        ]);
    }

    private static function InteractionInfo($uid)
    {
        //处理用户交互数据
        $counterArr = UserCounter::getOne($uid);
        //dd($counterArr);
        //从es中获取用户相关的数据
        //$es_info = EsUserPortrait::getList($uid);
        $es_info = PositionApi::getOneUserProfile($uid);
        //处理登录天数,没有测试和开发环境 先用mock数据
        //$login_days = rand(100,1000);
        $login_days = $es_info['rows'][0]['logindays'] ?: 0;
        //处理最后使用时间,使用es里的数据,开发环境和测试环境没有,使用mock数据
        $last_login_day = $es_info['rows'][0]['last_login_time'];
        //$last_login_day = 1598866678;
        $last_login_time = self::timeFormat($last_login_day);
        //处理关注数
        $follow_num = $counterArr['follow'];
        //处理关注话题数
        $follow_topic_num = $counterArr['follow_question'];
        //处理粉丝数
        $fans_num = $counterArr['fans'];
        //处理总收藏数
        $all_favorite_num = $counterArr['favorite_photo'] + $counterArr['favorite_article'] + $counterArr['favorite_guide'] + $counterArr['fav_spu'] + $counterArr['favorite_blank'] + $counterArr['favorite_answer'] + $counterArr['favorite_sitem'];
        //处理被收藏数
        $favorited_num = $counterArr['befavorited'];
        //处理创建灵感集的数量
        $ideabook_num = (int)IdeaBookModel::getCountIdeaBooksByUid($uid);
        //处理收藏的note数量
        $favorite_note_num = $counterArr['favorite_photo'];
        //处理收藏文章的数量
        $favorite_article_num = $counterArr['favorite_article'];
        //处理收藏回答的数量
        $favorite_answer_num = $counterArr['favorite_answer'];
        //点赞数
        $like_num = $counterArr['like'];
        //被点赞数
        $belieked_num = $counterArr['beliked'];

        //处理粉丝数的链接
        $fans_link = 'http://manage.haohaozhu.me/Statistics/UserStatistics/fansIncrement/keyword/' . $uid . '/search_type/uid';
        //处理发布的评论的链接
        $comment_link = "/admin/comment/index?&search[uid]=$uid";


        $interaction_info = [
            'Action_Day' => [
                'name' => '登录天数',
                'num' => $login_days,
            ],
            'Last_Use_Time' => [
                'name' => '最后使用时间',
                'num' => $last_login_time,
            ],
            'Follow_Num' => [
                'name' => '关注数',
                'num' => $follow_num,
            ],
            'Follow_Topic' => [
                'name' => '关注话题数',
                'num' => $follow_topic_num,
            ],
            'Fans_Num' => [
                'name' => '粉丝数',
                'num' => $fans_num,
                'link' => $fans_link,
            ],
            'All_Favorite_Num' => [
                'name' => '总收藏数',
                'num' => $all_favorite_num,
            ],
            'Favorited_Num' => [
                'name' => '被收藏数',
                'num' => $favorited_num,
            ],
            'Ideabook_Num' => [
                'name' => '创建灵感集数量',
                'num' => $ideabook_num,
            ],
            'Favorite_Note' => [
                'name' => '收藏note数',
                'num' => $favorite_note_num,
            ],
            'Favorite_Article' => [
                'name' => '收藏文章数',
                'num' => $favorite_article_num,
            ],
            'Favorite_Answer' => [
                'name' => '收藏回答数',
                'num' => $favorite_answer_num,
            ],
            'Like_Num' => [
                'name' => '点赞数',
                'num' => $like_num,
            ],
            'Liked_Num' => [
                'name' => '被点赞数',
                'num' => $belieked_num,
            ],
            'Comment' => [
                'name' => '发布的评论',
                'link' => $comment_link,
            ],
        ];
        return view('user::userdetail-interaction', [
            'interaction_info' => $interaction_info,
        ]);
    }

    private static function UserAction($uid)
    {
        return view('user::userdetail-action', [
            'uid' => $uid,
        ]);
    }

    protected static function timeFormat($time)
    {
        if ($time) {
            $time = time() - $time;
            if ($time > 86439) {
                return floor($time / 86400) . '天前';
            } elseif ($time > 3659) {
                return floor($time / 3600) . '小时前';
            } elseif ($time > 59) {
                return floor($time / 60) . '分钟前';
            } else {
                return $time . '秒前';
            }
        } else {
            return '';
        }
    }

    //处理管理记录的方法
    public static function OperateOrder(Request $request, $uid, WorkContent $content)
    {
        //获取某个管理员当前对该用户的管理记录
        //$admin_info = Auth::guard('admin')->user();
        //$admin_name = $admin_info->username;
        //$operate_info = AdmOperateLog::getLog($uid, $admin_name);
        //获取用户的所有操作记录
        $operate_info = AdmOperateLog::getOperateLog($uid);
        $operate_info = is_array($operate_info) ? $operate_info : [];
        foreach ($operate_info as &$operate_log) {
            switch ($operate_log['action']) {
                case AdmOperateLog::$operate_type['edit_nick']:
                    $operate_log['operate'] = '修改用户昵称（' . $operate_log['old_case'] . ' -> ' . $operate_log['new_case'] . ')';
                    break;
                case AdmOperateLog::$operate_type['user_auth']:
                    $operate_log['operate'] = '认证用户';
                    break;
                case AdmOperateLog::$operate_type['cancel_user_auth']:
                    $operate_log['operate'] = '取消用户认证';
                    break;
                case AdmOperateLog::$operate_type['ban_user']:
                    $operate_log['operate'] = '封禁用户';
                    break;
                case AdmOperateLog::$operate_type['cancel_ban_user']:
                    $operate_log['operate'] = '取消用户封禁';
                    break;
                case AdmOperateLog::$operate_type['frozen_user']:
                    $operate_log['operate'] = '冻结用户';
                    break;
                case AdmOperateLog::$operate_type['cancel_frozen_user']:
                    $operate_log['operate'] = '取消冻结用户';
                    break;
                case AdmOperateLog::$operate_type['send_emblem']:
                    $operate_log['operate'] = '下发徽章';
                    break;
                case AdmOperateLog::$operate_type['cancel_send_emblem']:
                    $operate_log['operate'] = '取消徽章';
                    break;
                case AdmOperateLog::$operate_type['edit_user_quality']:
                    $operate_log['old_case'] = empty($operate_log['old_case']) ? 0 : $operate_log['old_case'];
                    $operate_log['new_case'] = empty($operate_log['new_case']) ? 0 : $operate_log['new_case'];
                    $operate_log['old_case'] = self::$map_user_quality[$operate_log['old_case']];
                    $operate_log['new_case'] = self::$map_user_quality[$operate_log['new_case']];
                    $operate_log['operate'] = '运营标识（' . $operate_log['old_case'] . ' -> ' . $operate_log['new_case'] . ')';
                    break;
                default:
                    break;
            }
        }
        unset($operate_log);
        $total = count($operate_info);
        $gridModel = new GridDataModel($operate_info, $total, [
            'perPage' => $request['per_page'] ?: $request['LIMIT'][1],
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $uid) {
                $grid->disableExport();
                $grid->disableFilter();
                //筛选
                $grid->filter(function (\Encore\Admin\Grid\Filter $filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();

                });
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->disableRowSelector();
                //$grid->perPages([10, 20, 30, 40, 50, 100, 500, 1000, 2000, 5000]);
                $grid->disablePagination();

                $grid->column('time', '操作时间');
                $grid->column('admin_name', '管理员名称');
                $grid->column('operate', '具体操作');
            });

        return $content
            ->header('管理记录')
            ->description(' ')
            ->breadcrumb(['text' => '查看管理员的管理记录'])
            ->row($gridList);
    }

    //处理展示明文手机号的方法
    public static function ShowBindPhone(Request $request, $uid)
    {
        $phone = Phone::getDecryptPhoneByUid($uid, findPhoneScene::FRIEZA_USER_SHOW_BIND_PHONE);
        if (!$phone) {
            return view('user::userdetail-showbindphone', [
                'phone' => '手机号未绑定',
            ]);
        }

        //142的测试号所有人可查看
        if (HhzEnv::isProduction() && strpos($phone, "142") !== 0) {
            $list = [
                'zhouchao',
                'zhangzhijie',
                'liziyan', //可查看未认证设计师的手机号
            ];
            if (!in_array(Admin::user()->username, $list)) {
                return view('user::userdetail-showbindphone', [
                    'phone' => '您没有权限查看',
                ]);
            }
            $userInfo = Member::getUserInfo($uid);
            if (Admin::user()->username == 'liziyan' && $userInfo['type'] != Member::AUTH_USER_UNAUTH_DESIGNER) {
                return view('user::userdetail-showbindphone', [
                    'phone' => '您没有权限查看',
                ]);
            }
            $msg = Admin::user()->username . "查看了用户 {$userInfo['nick']} ({$uid}) 的手机号！";
            DingTalkAlert::sendTextMsg(DingTalkAlert::PHONE_CHECK, $msg);
        }

        $encryptPhoneId = Phone::addExtendPhone($phone, UserExtendPhoneScene::FRIEZA_SHOW_PHONE_HISTORY);
        AdmChkPhoneHistory::addHistory([
            'uid' => $uid,
            'admin_id' => Admin::user()->id,
            'phone' => $phone,
            'encrypt_phone_id' => $encryptPhoneId,
        ]);
        return view('user::userdetail-showbindphone', [
            'phone' => $phone,
        ]);
    }


    //处理展示用户所有徽章的方法
    public static function AllEmblem(Request $request, $uid, WorkContent $content)
    {
        $all_badge = TotemService::getAllGetTotem($uid);
        //dd($all_badge);
        $total = count($all_badge);
        $gridModel = new GridDataModel($all_badge, $total, [
            'perPage' => $request['per_page'] ?: $request['LIMIT'][1],
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableExport();
                $grid->disableFilter();
                //筛选
                $grid->filter(function (\Encore\Admin\Grid\Filter $filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();

                });
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->disableRowSelector();
                //$grid->perPages([10, 20, 30, 40, 50, 100, 500, 1000, 2000, 5000]);
                $grid->disablePagination();

                $grid->column('totem_img', '徽章图片')->display(function () {
                    return '<img src = "' . $this->totem_img . '" width = 80px height = 80px />';
                });;
                $grid->column('totem_name', '徽章名称');
//                $grid->column('badge_desc', '徽章说明');
                $grid->column('owned_time', '获得时间');
            });

        return $content
            ->header('用户所有徽章')
            ->description(' ')
            ->row($gridList);

    }

    public static function DecorationInfo(Request $request, $uid, WorkContent $content)
    {
        $decoration_records = MemberDecorationRecord::listByUid($uid);
        $decoration_records = is_array($decoration_records) ? $decoration_records : [];
        foreach ($decoration_records as &$item) {
            $item['decoration_status'] = empty($item['decoration_status']) ? 0 : $item['decoration_status'];
            $item['decoration_status_text'] = empty(MemberDecoration::DECORATION_STATUS[$item['decoration_status']]) ? '未选择' : MemberDecoration::DECORATION_STATUS[$item['decoration_status']];
            $item['area'] = PlaceArea::locationId2String($item['area']);

            // 户型
            if (!empty($item['house_type'])) {
                $item['house_type'] = '暂无';
            } else {
                $house_arr = explode(',', $item['house_type']);
                $item['house_type'] = $house_arr[0] . '室' . $house_arr[1] . '厅' . $house_arr[2] . '厨' . $house_arr[3] . '卫';
            }

            // 人口
            if (empty($item['people'])) {
                $item['people'] = '暂无';
            } else {
                $people_arr = explode(',', $item['people']);
                $item['people'] = $people_arr[0] . '成人' . $people_arr[1] . '老人' . $people_arr[2] . '儿童';
            }

            $item['space'] = $item['space'] . '平米';
            $item['budget'] = $item['budget'] . '万';
            $item['house_status'] = MemberDecoration::HOUSE_STATUS[$item['status']];
            $item['start_time'] = date('Y-m-d H:i:s', $item['start_time']);
            $item['check_in_time'] = date('Y-m-d H:i:s', $item['checkin_time']);
        }
        unset($item);
        $total = count($decoration_records);
        //dd($decoration_records);
        $gridModel = new GridDataModel($decoration_records, $total, [
            'perPage' => $request['per_page'] ?: $request['LIMIT'][1],
        ]);
        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel, $uid) {
                $grid->disableExport();
                $grid->disableFilter();
                //筛选
                $grid->filter(function (\Encore\Admin\Grid\Filter $filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();

                });
                $grid->disableCreateButton();
                $grid->disableActions();
                $grid->disableRowSelector();
                //$grid->perPages([10, 20, 30, 40, 50, 100, 500, 1000, 2000, 5000]);
                $grid->disablePagination();

                $grid->column('decoration_status_text', '装修进度');
                $grid->column('area', '房屋所在城市');
                $grid->column('house_type', '户型');
                $grid->column('space', '建筑面积');
                $grid->column('house_status', '建筑面积');
                $grid->column('people', '常住人口');
                $grid->column('budget', '装修预算');
                $grid->column('start_time', '开始装修时间');
                $grid->column('check_in_time', '计划入住时间');
                $grid->column('phone', '手机号码');
                $grid->column('remark', '备注');
                $grid->column('update_time', '修改时间');
//                $grid->header(function ($query) use ($uid) {
//                    return Admin::content(function (Content $content) use ($uid) {
//                        $content->header('用户现装修信息');
//                        $content->row(function (Row $row) use ($uid) {
//                            $row->column(4, function (Column $column) use ($uid) {
//                                $column->append(self::NowDecorationInfo($uid));
//                            });
//                        });
//                    });
//                });
            });

        return $content
            ->header('用户装修信息记录')
            ->description(' ')
            ->row(function (Row $row) use ($uid) {
                $row->column(8, function (Column $column) use ($uid) {
                    $column->append(self::NowDecorationInfo($uid));
                });
            })
            ->row($gridList);
    }

    public static function NowDecorationInfo($uid)
    {
        $decoration_info = MemberDecoration::getInfo($uid);
        $decoration_info['decoration_status'] = MemberExtendInfo::getDecorationStatus($uid);
        // 所在城市
        $decoration_info['area'] = PlaceArea::locationId2String($decoration_info['area']);
        // 装修状态
        $decoration_info['decoration_status_text'] = empty(MemberDecoration::DECORATION_STATUS[$decoration_info['decoration_status']]) ? '未选择' : MemberDecoration::DECORATION_STATUS[$decoration_info['decoration_status']];
        // 户型
        if (!empty($decoration_info['house_type'])) {
            $house_arr = explode(',', $decoration_info['house_type']);
            $decoration_info['house_type'] = $house_arr[0] . '室' . $house_arr[1] . '厅' . $house_arr[2] . '厨' . $house_arr[3] . '卫';
        } else {
            $decoration_info['house_type'] = '暂无';
        }
        if (empty($decoration_info['people'])) {
            $decoration_info['people'] = '暂无';
        } else {
            $people_arr = explode(',', $decoration_info['people']);
            $decoration_info['people'] = $people_arr[0] . '成人' . $people_arr[1] . '老人' . $people_arr[2] . '儿童';
        }
        $decoration_info['space'] = $decoration_info['space'] . '平米';
        $decoration_info['budget'] = $decoration_info['budget'] . '万';
        $decoration_info['house_status'] = MemberDecoration::HOUSE_STATUS[$decoration_info['status']];
        $decoration_info['start_time'] = date('Y-m-d H:i:s', $decoration_info['start_time']);
        $decoration_info['check_in_time'] = date('Y-m-d H:i:s', $decoration_info['checkin_time']);
        $dec_info = [
            ['name' => '装修进度', 'value' => $decoration_info['decoration_status_text']],
            ['name' => '房屋所在城市', 'value' => $decoration_info['area']],
            ['name' => '户型', 'value' => $decoration_info['house_type']],
            ['name' => '建筑面积', 'value' => $decoration_info['space']],
            ['name' => '房屋现状', 'value' => $decoration_info['house_status']],
            ['name' => '常住人口', 'value' => $decoration_info['people']],
            ['name' => '装修预算', 'value' => $decoration_info['budget']],
            ['name' => '开始装修时间', 'value' => $decoration_info['start_time']],
            ['name' => '计划入住时间', 'value' => $decoration_info['check_in_time']],
            ['name' => '手机号码', 'value' => sMember\PhoneCrypto::mask($decoration_info['phone'])],
            ['name' => '备注', 'value' => $decoration_info['remark']],

        ];


        return view('user::userdetail-decinfo', [
            'dec_info' => $dec_info,
        ]);
    }

}
